#include "tcStateToy.h"
#include <GL/freeglut.h>
#include <juanmabc.h>
#include "tcGame.h"
#include "tcStateMain.h"
#include "tcStateRace.h"

void
tcStateToyIn(void)
{
	TCGame *game;

	game = tcGame();

	tlGenStrings(2, game->toy.strs);
	tlStringLoad(game->toy.strs[0], L"Toy:");
}

void
tcStateToyOut(void)
{
	TCGame *game;

	game = tcGame();

	tlStringUnload(game->toy.strs[0]);
	tlDeleteStrings(2, game->toy.strs);
}

void
tcStateToyKeyboard(unsigned char key, int x, int y)
{
	TCGame *game;

	game = tcGame();
}

void
tcStateToyKeyboardUp(unsigned char key, int x, int y)
{
	TCGame *game;

	game = tcGame();

	switch(key)
	{
		case 27:
			alSourcePlay(game->general.sounds[2]);
			tcStateToyOut();
			game->state = TC_STATE_MAIN;
			tcStateMainIn();

		break;
		case 13:
			alSourcePlay(game->general.sounds[2]);
			tcStateToyOut();
			game->state = TC_STATE_RACE;
			tcStateRaceIn();
		break;
		default:
		break;
	}
}

void
tcStateToySpecial(int key, int x, int y)
{
	TCGame *game;

	game = tcGame();

	switch(key)
	{
		case GLUT_KEY_LEFT:
			alSourcePlay(game->general.sounds[1]);
			game->options.race.toy = (game->options.race.toy + game->general.ntoys - 1) % game->general.ntoys;
		break;
		case GLUT_KEY_RIGHT:
			alSourcePlay(game->general.sounds[1]);
			game->options.race.toy = (game->options.race.toy + 1) % game->general.ntoys;
		break;
		default:
		break;
	}
}

void
tcStateToySpecialUp(int key, int x, int y)
{
	TCGame *game;

	game = tcGame();
}

void
tcStateToyFrame(void)
{
	TCGame *game;
	unsigned int i;
	unsigned int w, h;
	float tw, th;
	wchar_t buffer[128];

	game = tcGame();

	swprintf(buffer, 128, L"<%ls>", tlStringData(game->general.toys[game->options.race.toy]));
	tlStringLoad(game->toy.strs[1], buffer);

	gluLookAt(0.0f, 0.0f, 250.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); 

	tw = th = 0.0f;
	for(i = 0; i < 2; i++)
	{
		flFontStringMetrics(game->general.font, game->toy.strs[i], &w, &h);
		tw = tw >= w ? tw : w;
		th += i < 2 - 1 ? (float) h * 1.5f : (float) h;
	}

	glPushMatrix();
	glScalef(0.75f * game->general.display.aspect, 0.75f * game->general.display.aspect, 0.75f * game->general.display.aspect);
	glTranslatef(0.0f, th / 2.0f, 0.0f);
	for(i = 0; i < 2; i++)
	{
		flFontStringMetrics(game->general.font, game->toy.strs[i], &w, &h);

		glDisable(GL_CULL_FACE);
		glEnable(GL_TEXTURE_GEN_S);
		glEnable(GL_TEXTURE_GEN_T);
		glMatrixMode(GL_TEXTURE);
		glPushMatrix();
		glScalef(0.1f, 0.1f, 0.1f);
		glMatrixMode(GL_MODELVIEW);

		mlMaterialBind(game->general.fontmat);

		glPushMatrix();
		glTranslatef(- (float) w / 2.0f, - (float) h, 0.0f);
		flFontDrawStringFill(game->general.font, game->toy.strs[i]);
		glPopMatrix();

		glMatrixMode(GL_TEXTURE);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);
		glDisable(GL_TEXTURE_GEN_S);
		glDisable(GL_TEXTURE_GEN_T);
		glEnable(GL_CULL_FACE);

		if(i == 1)
		{
			glPushMatrix();
			glTranslatef(- (float) w / 2.0f - 10.0f, - (float) h / 2.0f, 0.0f);
			glRotatef(-90.0f, 0.0f, 1.0f, 0.0f);
			glRotatef(- (float) (game->general.display.frame * 10 % 360), 0.0f, 0.0f, 1.0f);
			glScalef(2.0f, 2.0f, 2.0f);
			mlMeshDraw(game->general.selector);
			glPopMatrix();

			glPushMatrix();
			glTranslatef((float) w / 2.0f + 10.0f, - (float) h / 2.0f, 0.0f);
			glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
			glRotatef((float) (game->general.display.frame * 10 % 360), 0.0f, 0.0f, 1.0f);
			glScalef(2.0f, 2.0f, 2.0f);
			mlMeshDraw(game->general.selector);
			glPopMatrix();
		}

		glTranslatef(0.0f, - (float) h * 1.5f, 0.0f);

	}
	glPopMatrix();

	tlStringUnload(game->toy.strs[1]);

}
